<?php
/**
 * This file is form http://findcat.cn
 *
 * @link     https://findcat.cn
 * @email    1476982312@qq.com
 */
namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $guarded = [];

    /**
     * 声明可以编辑的字段.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * 隐藏密码和token字段.
     *
     * @var array
     */
    protected $hidden = [
        //        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * 用户关联文章.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function article()
    {
        return $this->hasMany(Article::class, 'id', 'user_id');
    }

    /**
     * 用户关联评论.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function comment()
    {
        return $this->hasMany(Comment::class);
    }

    /**
     * 用户评论数.
     *
     * @param $userId
     *
     * @return int
     */
    public function commentCount($userId)
    {
        return $this->comment()->where('user_id', $userId)->count();
    }

    /**
     * 用户关联友情链接.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function friendLink()
    {
        return $this->hasOne(FriendlyLink::class, 'id', 'user_id');
    }

    /**
     * 第三方登录.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function userOath()
    {
        return $this->hasMany(UserOath::class);
    }
}
